package com.xioz.android;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;

/**
 * TimeAttackGameScreen Class
 * This is a subclass of WordGameScreen, simply used to show
 * a particular game in which the player destroys as many tiles
 * in a set time
 * 
 * @author Zong Xiong
 */
public class TimeAttackGameScreen extends WordGameScreen {
	
/***************************************************
 * Fields
 ***************************************************/
	
	// time limit player has to destroy tiles
	private long TIME_LIMIT = 30000;
	// used to store and load scores
	private Preferences prefs;
	// tracks score placement after goal is met
	private int scorePlacement = 0;

/***************************************************
 * Public Methods
 ***************************************************/
	
	/**
	 * Default Constructor
	 * calls superclass constructor then own initialization
	 */
	public TimeAttackGameScreen() {
		super();
		timeAttackGameInitialized();
	}

	/**
	 * Constructor with parent Game
	 * calls superclass constructor then own initialization
	 * @param inGame , parent game
	 */
	public TimeAttackGameScreen(Game inGame) {
		super();
		timeAttackGameInitialized();
	}

/***************************************************
 * Protected Methods
 ***************************************************/

	/**
	 * Modified draw method
	 * Adds a line of text for score display
	 */
	protected void draw() {
		super.draw();
		printBoard("Time:" + gameTimer.printTime() + "   Tiles:" + (tilesScore) , 20, 79);
	}

	/**
	 * Modified update method
	 * Tracks if goals are met for game and if so ends game
	 */
	protected void update() {
		super.update();
		
		// checks if the timer has run out, if so ends the game and checks if player
		// has charted with his score
		if (gameTimer.getTime()==0) {
			super.loseGame();
			
			// checks if the score has charted 
			for (int i=1; i<6 && scorePlacement == 0; i++) {
				if (tilesScore > prefs.getLong("Place" + i + "Score")) {
					scorePlacement = i;
					shiftScores(i);
					prefs.putLong("Place" + i + "Score", tilesScore);
				}
			}
			// if charted, prompts player for name
			if (scorePlacement > 0) {
				super.enterHighScore();
				prefs.putString("Place" + scorePlacement + "Name", super.getScoreName() );
				prefs.flush();
			}
		}
	}
	
/***************************************************
 * Private Methods
 ***************************************************/
	
	/**
	 * ShiftScores
	 * shifts scores downward from scorePlacement
	 * @param scorePlacement, place acquired by player
	 */
	private void shiftScores(int scorePlacement) {
		for (int i = 5; i > scorePlacement; i--) {
			prefs.putLong("Place" + (i) + "Score", prefs.getLong("Place" + (i-1) + "Score"));
			prefs.putString("Place" + (i) + "Name", prefs.getString("Place" + (i-1) + "Name"));
		}
	}
	
	/**
	 * timeAttackGameInitialized
	 * initializes a timeAttack game
	 */
	private void timeAttackGameInitialized() {
		// set timer
		gameTimer.setTime(TIME_LIMIT );
		gameTimer.setCountUp(false);
		// loads scores
		prefs = Gdx.app.getPreferences("TimeAttackWordGame");
	}

}
